package com.tarena.lbs.business.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.business.dao.mapper.BusinessMapper;
import com.tarena.lbs.business.dao.mapper.StoreMapper;
import com.tarena.lbs.business.dao.repository.StoreRepository;
import com.tarena.lbs.business.pojo.param.StoreParam;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.AreaStoreQuery;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


import java.util.Date;
import java.util.List;

@Repository
@Slf4j
public class StoreRepositoryImpl implements StoreRepository {
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public void insertStore(StorePO storePO) {
        log.info("插入数据：{}", storePO);
        storeMapper.insert(storePO);
    }

    @Override
    public BusinessPO selectBusinessById(Integer businessId) {
        log.info("查询商家信息：{}", businessId);
        return businessMapper.selectById(businessId);
    }

    @Override
    public List<StorePO> selectByQuery(Integer businessId) {
        log.info("66666查询店铺信息：{}", businessId);
        QueryWrapper<StorePO> wrapper = new QueryWrapper<>();
        wrapper.eq("business_id", businessId);
        StoreQuery query = new StoreQuery();
        if(query.getStoreName() != null){
            wrapper.like("store_name", query.getStoreName());
        }
        if(query.getStoreHeadPhone() != null){
            wrapper.like("store_head_phone", query.getStoreHeadPhone());
        }
        if(query.getAreaId() != null){
            wrapper.eq("area_id", query.getAreaId());
        }
        if(query.getStartingTime() != null){
            wrapper.ge("update_time", query.getStartingTime());
        }
        if(query.getStoreStatus() != null){
            wrapper.eq("store_status", query.getStoreStatus());
        }
//        log.info("查询条件：{}", wrapper.eq("business_id", businessId));
        List<StorePO> storePOS = storeMapper.selectList(wrapper);// 确保此方法存在
        log.info("6666查询结果：{}", storePOS);
        return storePOS;
    }

    @Override
    public List<StorePO> selectByBusinessAndAreas(AreaStoreQuery  query) {
        log.info("查询店铺信息：{}",  query);
        QueryWrapper<StorePO> wrapper = new QueryWrapper<>();
        List<Long> cityIdList = query.getCityIdList();
        List<StorePO> storePOS = storeMapper.selectList(wrapper);
        log.info("查询结果：{}", storePOS);
        return storePOS;
    }

    @Override
    public void deleteStore(Long id) {
        log.info("删除店铺信息：{}", id);
        storeMapper.deleteById(id);
        log.info("删除结果：{}", id);
    }

    @Override
    public StorePO selectStore(Long id) {
        log.info("查询店铺信息：{}", id);
        StorePO storePO = storeMapper.selectById(id);
        return storePO;
    }

    @Override
    public void updateStore(StoreParam param) {
        log.info("更新店铺信息：{}", param);
        StorePO storePO = new StorePO();
        BeanUtils.copyProperties(param, storePO);
        storePO.setUpdateTime(new Date());
        storeMapper.updateById(storePO);
    }

    @Override
    public void enableStore(Long id) {
        StorePO storePO = storeMapper.selectById(id);
        if(storePO.getStoreStatus() == 0){
            storePO.setStoreStatus(-1);
        }else {
            storePO.setStoreStatus(0);
        }
        storeMapper.updateById(storePO);
    }

}
